package com.lxy.cxcyxmmsbackend.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxy.cxcyxmmsbackend.sys.entity.QxglUser;
import com.lxy.cxcyxmmsbackend.sys.entity.XmCxsq;
import com.lxy.cxcyxmmsbackend.sys.entity.XmJdjl;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.req.PageReq;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.QxglUserService;
import com.lxy.cxcyxmmsbackend.sys.service.XmJdjlService;
import com.lxy.cxcyxmmsbackend.sys.utils.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 进度记录
 * @author 刘新颖
 * @since 2024-01-07
 */
@Api(tags = "项目进度检查")
@RestController
@RequestMapping("/xmJdjl")
public class XmJdjlController {

    @Autowired
    XmJdjlService xmJdjlService;

    @Autowired
    QxglUserService qxglUserService;

    @ApiOperation("查询项目进度记录")
    @PostMapping("/queryXmJdjl")
    @Transactional
    public Result queryXmJdjl(@RequestBody PageReq pageReq, HttpServletRequest request){

        Integer limitS = pageReq.getCurrentPage();
        Integer pageS = pageReq.getPageSize();
        Page<XmJdjl> pages = new Page<>(limitS, pageS);
        QueryWrapper<XmJdjl> wrapper = new QueryWrapper<>();
        String xmmc = pageReq.getKeyWord().get("xmmc");
        String jcsj = pageReq.getKeyWord().get("jcsj");
        String xmBh = pageReq.getKeyWord().get("xmBh");
        if (xmBh!=null && !xmBh.isEmpty() && !"null".equals(xmBh)){
            wrapper.lambda().eq(XmJdjl::getXmBh,xmBh);
        }
        //项目名称
        if (xmmc!=null && !"".equals(xmmc)){
            wrapper.lambda().eq(XmJdjl::getXmMc,xmmc);
        }

        //项目状态
        if (jcsj!=null && !"".equals(jcsj)){
            wrapper.lambda().eq(XmJdjl::getXmJcsj,jcsj);
        }

        wrapper.orderByDesc("XM_JCSJ");

        IPage<XmJdjl> page = xmJdjlService.page(pages, wrapper);

        HashMap<Object, Object> map = new HashMap<>();

        map.put("pageNo",limitS);
        map.put("pageSize",pageS);
        List<XmJdjl> records = page.getRecords();

        map.put("rows",records);
        map.put("total",page.getTotal());
        map.put("pageNums",(int)Math.ceil((double) page.getTotal()/pageS));

        return Result.success("查询项目进度检查记录成功",map);
    }

    @ApiOperation("新增项目进度检查记录")
    @PostMapping("/addXmJdjl")
    @Transactional
    public Result addXmJdjl(@RequestBody XmJdjl xmJdjl, HttpServletRequest request){
        String userBh = request.getHeader("userBh");
        QxglUser user = qxglUserService.getById(userBh);
        xmJdjl.setJdjlBh(UUIDUtil.getUUID8("JDJL",8));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        xmJdjl.setXmJcsj(format.format(date));
        xmJdjl.setStatus("0");
        xmJdjl.setTjrBh(userBh);
        xmJdjl.setTjrMc(user.getUserMc());
        boolean save = xmJdjlService.save(xmJdjl);
        if (!save){
            throw new NormalException("新增项目进度检查记录失败");
        }
        return Result.success("新增项目进度检查记录成功",null);
    }

    @ApiOperation("修改项目进度检查记录")
    @PostMapping("/updateXmJdjl")
    @Transactional
    public Result updateXmJdjl(@RequestBody XmJdjl xmJdjl){
        boolean save = xmJdjlService.updateById(xmJdjl);
        if (!save){
            throw new NormalException("修改项目进度检查记录失败");
        }
        return Result.success("修改项目进度检查记录成功",null);
    }

    @ApiOperation("审批项目进度检查记录")
    @PostMapping("/approvalXmJdjl")
    @Transactional
    public Result approvalXmJdjl(@RequestBody XmJdjl xmJdjl, HttpServletRequest request){
        String userBh = request.getHeader("userBh");
        QxglUser user = qxglUserService.getById(userBh);
        xmJdjl.setSprBh(user.getUserBh());
        xmJdjl.setSprMc(user.getUserMc());
        boolean save = xmJdjlService.updateById(xmJdjl);
        if (!save){
            throw new NormalException("审批项目进度检查记录失败");
        }
        return Result.success("审批项目进度检查记录成功",null);
    }

    @ApiOperation("删除项目进度检查记录")
    @PostMapping("/removeXmJdjl/{bh}")
    @Transactional
    public Result removeXmJdjl(@PathVariable("bh") String bh){
        boolean remove = xmJdjlService.removeById(bh);
        if (!remove){
            throw new NormalException("删除项目进度检查记录失败");
        }
        return Result.success("删除项目进度检查记录成功",null);
    }

    @ApiOperation("查看项目进度检查记录详情")
    @GetMapping("/getXmJdjlDetail/{bh}")
    @Transactional
    public Result getXmJdjlDetail(@PathVariable("bh") String bh){
        XmJdjl detail = xmJdjlService.getById(bh);
        if (detail == null){
            throw new NormalException("查询项目进度检查记录详情失败");
        }
        return Result.success("查询项目进度检查记录详情成功",detail);
    }
}

